<template>
  <div class="roles">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 添加角色按钮区域 -->
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 角色列表区域 -->
      <el-table :data="list" border stripe>
        <!-- 展开列 -->
        <el-table-column type="expand">
          <template #default="{ row: role }">
            <el-alert
              v-if="!role.children || !role.children.length"
              title="暂无数据"
              type="info"
              show-icon
              :closable="false"
            ></el-alert>
            <div v-else class="expand_column_container">
              <el-row
                v-for="(v, i) in role.children"
                :key="v.id"
                :class="[i === 0 ? 'bt1px' : '', 'bb1px', 'vcenter']"
              >
                <el-col :span="5">
                  <el-tag closable @close="delRight(role, v.id)">{{
                    v.authName
                  }}</el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <el-col :span="19">
                  <el-row
                    v-for="(v2, i2) in v.children"
                    :key="v2.id"
                    :class="[i2 !== 0 ? 'bt1px' : '', 'vcenter']"
                  >
                    <el-col :span="8">
                      <el-tag
                        type="success"
                        closable
                        @close="delRight(role, v2.id)"
                      >
                        {{ v2.authName }}
                      </el-tag>
                      <i class="el-icon-caret-right"></i>
                    </el-col>
                    <el-col :span="16">
                      <el-tag
                        v-for="v3 in v2.children"
                        :key="v3.id"
                        type="warning"
                        closable
                        @close="delRight(role, v3.id)"
                      >
                        {{ v3.authName }}
                      </el-tag>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <!-- 索引列 -->
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作">
          <template #default="{ row: role }">
            <el-button type="primary" icon="el-icon-edit" size="mini"
              >编辑角色</el-button
            >
            <el-button type="danger" icon="el-icon-delete" size="mini"
              >删除角色</el-button
            >
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="goAllocRights(role)"
            >
              分配权限
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      title="分配权限"
      :visible.sync="allocRightsDialog.visible"
      @closed="allocRightsDialog.defCheckedKeys = []"
    >
      <template #default>
        <el-tree
          v-if="allocRightsDialog.visible"
          ref="rightTree"
          :data="allocRightsDialog.rightTree"
          :props="{ label: 'authName', children: 'children' }"
          show-checkbox
          default-expand-all
          node-key="id"
          :default-checked-keys="allocRightsDialog.defCheckedKeys"
        ></el-tree>
      </template>
      <template #footer>
        <el-button @click="allocRightsDialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="allocRights">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Rights',
  data() {
    return {
      allocRightsDialog: {
        visible: false,
        role: null,
        rightTree: null,
        defCheckedKeys: [],
      },
      list: [],
    };
  },
  methods: {
    // 分配权限
    allocRights() {
      const rightIdsStr = [
        ...this.$refs.rightTree.getCheckedKeys(),
        ...this.$refs.rightTree.getHalfCheckedKeys(),
      ].join(',');
      this.__apis.role
        .allocRights(this.allocRightsDialog.role.id, rightIdsStr)
        .then((res) => {
          if (res.data.meta.status === 200) {
            this.$message.success('权限分配成功');
            this.allocRightsDialog.visible = false;
            this.getRoles();
          } else {
            this.$message.error(res.data.meta.msg);
          }
        });
    },
    // 加载默认选中的key
    loadDefCheckedKeys(node) {
      if (!node.children || !node.children.length) {
        this.allocRightsDialog.defCheckedKeys.push(node.id);
      } else {
        node.children.forEach((v) => {
          this.loadDefCheckedKeys(v);
        });
      }
    },
    // 加载权限树数据
    getRightTree() {
      return this.__apis.right.list('tree').then((res) => {
        if (res.data.meta.status === 200) {
          this.allocRightsDialog.rightTree = res.data.data;
          return res.data.data;
        } else {
          this.$message.error(res.data.meta.msg);
          throw res.data.meta.msg;
        }
      });
    },
    // 显示分配权限对话框
    goAllocRights(role) {
      new Promise((resolve, reject) => {
        if (!this.allocRightsDialog.rightTree) {
          // 如果是第一次显示对话框，就在这里请求权限树数据
          resolve(this.getRightTree());
        } else {
          // 如果已请求过一次，就不用再请求了
          resolve();
        }
      }).then(() => {
        // 保存当前操作的角色
        this.allocRightsDialog.role = role;
        // 加载默认选中的key
        this.loadDefCheckedKeys(role);
        // 显示对话框
        this.allocRightsDialog.visible = true;
      });
    },
    // 删除权限
    delRight(role, rightId) {
      this.__apis.role.delRight(role.id, rightId).then((res) => {
        if (res.data.meta.status === 200) {
          role.children = res.data.data;
          this.$message.success('删除成功');
        } else {
          this.$message.error(res.data.meta.msg);
        }
      });
    },
    // 获取角色列表数据
    getRoles() {
      this.__apis.role.list().then((res) => {
        if (res.data.meta.status === 200) {
          this.list = res.data.data;
        } else {
          this.$message.error(res.data.meta.msg);
        }
      });
    },
  },
  created() {
    // 获取角色列表数据
    this.getRoles();
  },
};
</script>

<style lang="less" scoped>
.roles {
  .el-card {
    margin-top: 20px;
    .el-table {
      margin-top: 20px;
      .expand_column_container {
        .el-tag {
          margin: 5px;
        }
      }
    }
  }

  .bt1px {
    border-top: 1px solid #eee;
  }

  .bb1px {
    border-bottom: 1px solid #eee;
  }

  .vcenter {
    display: flex;
    align-items: center;
  }
}
</style>
